package com.cqut.forum.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.Role;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.UserService;
import com.cqut.forum.util.ResponsePrintWriter;

/**
 * 用户登录controller
 * @author yangxun
 */
@Controller
public class LoginAction {
	private String userName;
	private String userPassword;
	private String number;
	@Resource
	private UserService userService;
	
	@RequestMapping("/main/login")
	public void loginAction(HttpServletRequest request,HttpServletResponse response,
			String userName,String userPassword,String number) throws ServletException, IOException{
		if(userName==null||userPassword==null){
			String json = "alert('登录失败');";
			ResponsePrintWriter.printWriterString(response, json);
		}
		User user = userService.login(userName,userPassword);
		if(user !=null){
			//String json = "window.location = '/networkforum/pages/user/user_main.html'";
			String json = "" +
					"(function(){" +
						//"var toper = document.getElementById('tnav');" +
						//"toper.src = '/networkforum/pages/user/user_header.html';" +
						//"toper.style.display = '';" +
						//"document.getElementById('header').style.marginTop = '25px!important';" +
						"document.getElementById('header').removeChild(document.getElementById('login'));" +
						"var ifm = document.getElementById('tnav');" +
						"var name = ifm.contentDocument.getElementById('username');" +
						"name.innerHTML = '"+user.getUserName() +"';"+
						"name.parentElement.style.display = '';"+
					"})()";
			request.getSession().setAttribute("user", user);
			ResponsePrintWriter.printWriterString(response, json);
		}
		else{
			String json = "alert('登录失败');";
			ResponsePrintWriter.printWriterString(response, json);
		}
	}
	@RequestMapping("/main/login2")
	public void login2Action(HttpServletRequest request,HttpServletResponse response,
			String userName,String userPassword,String number) throws ServletException, IOException{
		if(userName==null||userPassword==null){
			String json = "alert('登录失败');";
			ResponsePrintWriter.printWriterString(response, json);
		}
		User user = userService.login(userName,userPassword);
		if(user !=null){
			request.getSession().setAttribute("user", user);
			ResponsePrintWriter.printWriterString(response, "true");
		}
		else{
			ResponsePrintWriter.printWriterString(response, "false");
		}
	}
	@RequestMapping("/main/control_login")
	@ResponseBody
	public String control_loginAction(HttpServletRequest request,
			String userName,String userPassword){
		if(userName==null||userPassword==null){
			return "false";
		}
		//int roleId = userService.getUserRoleId("管理员");
		User user = userService.login(userName,userPassword);
		//userService.getUserRoleId(roleName)
		Role role = user.getRole();
		if("会员".equals(role.getRoleName())){
			return "noLevel";
		}
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
		return "true";
	}
	
	
	/***************get and set method*********************/
	public UserService getUserService() {
		return userService;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
}
